package cn.edu.nuc.film.controller;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.entity.Function;
import cn.edu.nuc.film.service.interfaces.FunctionService;

@Controller
@RequestMapping(value="/film/function")
public class FunctionController {
	
	@Autowired
	private FunctionService functionService;
	
	/**
	 * 功能列表
	 * @return
	 */
	@RequestMapping(value="/functionlist")
	public String list(String sname,HttpServletRequest request){
		// 获取要查询的名字
		if (sname == null || sname.equals("")) {
			sname = null;
		}
		// 设置分页信息
		PageControl pc = new PageControl();

		// 设置初始当前页currindex = 1
		Integer currindex = 1;

		// 如果获取的从前台获取的index(当前页)不为空，则赋值给 currindex(分页当前页)
		if (request.getParameter("index") != null) {
			currindex = Integer.parseInt(request.getParameter("index"));
		}

		// 将获取的当前页设置在分页信息(PageControl)里
		pc.setCurrentindex(currindex);
		pc.setPagesize(5);

		// 调用service中的分页方法，获取分页信息
		Pager<Function> pager = functionService.funlist(pc, sname);

		// 将分页信息传送到前台
		request.setAttribute("pager", pager);
		return "sys/function/list";
	}
	
	/**
	 * 进入添加功能的jsp界面
	 * @param pid    父功能id
	 * @param pname  父功能名称
	 * @param model  
	 * @return
	 */
	@RequestMapping(value="/toaddFun",method=RequestMethod.GET)
	public String toaddFun(int pid,String pname,Model model){
		model.addAttribute("pid", pid);
		model.addAttribute("pname", pname);
		return "sys/function/add";
	}
	
	/**
	 * 执行添加操作，保存添加的功能信息
	 * @param fun    function对象->（获取的功能信息）
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addFun",method=RequestMethod.POST)
	public String addFun(Function fun,Model model){
		int count = functionService.addFun(fun);
		if( count > 0){
			return "redirect:functionlist";
		}
		return "sys/function/add";
	}
	
	/**
	 * 通过fid(功能id)来获取功能的详细信息
	 * @param fid(功能id)
	 * @return
	 */
	@RequestMapping(value="/detailFun",method=RequestMethod.GET)
	public String detailFun(int fid,Model model){
		Function function = functionService.detailFun(fid);
		model.addAttribute("item", function);
		return null;
	}
	
	/**
	 * 进入编辑功能的jsp界面
	 * @param fid   功能id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toeditFun",method=RequestMethod.GET)
	public String toeditFun(int fid,Model model){
		Function function = functionService.detailFun(fid);
		model.addAttribute("item", function);
		return "sys/function/edit";
	}
	
	/**
	 * 执行编辑功能
	 * @param function
	 * @return
	 */
	@RequestMapping(value="/editFun",method=RequestMethod.POST)
	public String editFun(Function function){
		int count = functionService.editFun(function);
		if( count > 0){
			return "redirect:functionlist";
		}else{
			return "sys/function/edit";
		}
	}
	
	@RequestMapping(value="/deleteFun",method=RequestMethod.GET)
	public String deleteFun(int fid,Model model){
		int count = functionService.deleteFun(fid);
		if( count > 0){
			return "redirect:functionlist";
		}else{
			model.addAttribute("msg", "删除失败");
			return "redirect:functionlist";
		}
		
	}
	
	
}
